package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.basketball.JCLQSelectPlayMethodPop;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallClickListener;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.activity.trend.PL3TrendActivity;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.betzhushu.ZhuShuFactory;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.L;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;

import java.util.ArrayList;
import java.util.List;


public abstract class PL3AndFc3DBaseActivity extends RightPopLotteryActivity implements
        BallClickListener, BallItemClickLitener, JCLQSelectPlayMethodPop.OnWFCheckChangedListener {

    private BallPopFrameLayout moveQiu;
    private DrawBallFrameLayout ball_linear;
    private BallScrollView ball_scroll;

    SelectBallView ballView_firstLine;
    SelectBallView ballView_twoline;
    SelectBallView ballView_threeline;

    private LinearLayout shake_random_ll;

    private PopFC3DSelectPlayMethod popFC3DSelectPlayMethod;

    List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
    ;
    List<BallInfo> balls_threeLine = new ArrayList<BallInfo>();
    ;

    private TextView firstTitle, twoTitle, threeTitle;

    private TextView bottom_zhushu;
    private TextView bottom_money;
    ;
    // 新加
    protected LinearLayout ll_history;

    protected final int ZHIXUAN = 0x1001;
    protected final int ZUSANDANSHI = 0x1002;
    protected final int ZUSANFUSHI = 0x1003;
    protected final int ZULIU = 0x1004;

    protected int string_zhixuan = R.string.fc3d_zhixuan;
    protected int string_zusandan = R.string.fc3d_zusandanshi;
    protected int string_zusanfu = R.string.fc3d_zusanfushi;
    protected int string_zuliu = R.string.fc3d_zuliu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if(!isPL3()) {
            setContentView(R.layout.dj_activity_pl3);
        } else {
            setContentView(R.layout.dj_activity_pl3_new);
        }
        lottery_wafa = ZHIXUAN;
        initGuanjianZi();
        initBallView();

//        popupClickItem(popupId);
//        ballClick();
    }

    protected boolean isPL3(){
        return false;
    }

    @Override
    public TopTitleView getTopView() {
        return topView;
    }

    public void initGuanjianZi() {
        // TODO Auto-generated method stub

    }

    public void initBallView() {
        TouZhuHelper.getInstance().setBallClickListener(this);
        TouZhuHelper.getInstance().setBallItemClickLitener(this);

        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setCenterText(string_zhixuan);
        topView.setJianTouVisible(View.VISIBLE);
        topView.setOnTitleClickListener(this);

        shake_random_ll = (LinearLayout) findViewById(R.id.shake_random_ll);
        shake_random_ll.setOnClickListener(this);


        popFC3DSelectPlayMethod = new PopFC3DSelectPlayMethod(this, AbsListView.LayoutParams
                .MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,isPL3());
        popFC3DSelectPlayMethod.setOnWFCheckChangedListener(this);
        moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
        ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
        ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
        ball_linear.setScr(ball_scroll);
        ball_linear.setFl_moveqiu(moveQiu);
        ball_linear.setUpOrDownListener(this);

        firstTitle = (TextView) findViewById(R.id.firstlineTitle);
        twoTitle = (TextView) findViewById(R.id.twolineTitle);
        threeTitle = (TextView) findViewById(R.id.threelineTitle);
        ballView_firstLine = (SelectBallView) findViewById(R.id.firstLine);
        ballView_twoline = (SelectBallView) findViewById(R.id.twoLine);
        ballView_threeline = (SelectBallView) findViewById(R.id.thressLine);
        bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
        bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);
        ballView_firstLine.setShowMiss(!isPL3());
        ballView_twoline.setShowMiss(!isPL3());
        ballView_threeline.setShowMiss(!isPL3());
        // 新加
        ll_history = (LinearLayout) findViewById(R.id.pl3_ball_history);

        initArrayInfos();

        initBottomView();
    }

    @Override
    protected void initArrayInfos() {
        if (arr_infos.size() == 3) {
            balls_firstLine = arr_infos.get(0);
            balls_twoLine = arr_infos.get(1);
            balls_threeLine = arr_infos.get(2);
        } else {
            arr_infos.clear();
            initBallList(balls_firstLine, 10, 0, true);
            initBallList(balls_twoLine, 10, 0, true);
            initBallList(balls_threeLine, 10, 0, true);
            arr_infos.add(balls_firstLine);
            arr_infos.add(balls_twoLine);
            arr_infos.add(balls_threeLine);
        }

        ballView_firstLine.setBallInfoList(balls_firstLine);
        ballView_twoline.setBallInfoList(balls_twoLine);
        ballView_threeline.setBallInfoList(balls_threeLine);
    }

    public void setLotteryBallView(int lotteryWaFa) {
        switch (lotteryWaFa) {
            case ZHIXUAN:
                lottery_wafa = ZHIXUAN;
                firstTitle.setVisibility(View.VISIBLE);
                twoTitle.setVisibility(View.VISIBLE);
                threeTitle.setVisibility(View.VISIBLE);
                firstTitle.setText(R.string.bai);
                twoTitle.setText(R.string.shi);
                threeTitle.setText(R.string.ge);
                topView.setCenterText(string_zhixuan);
                ballView_firstLine.setVisibility(View.VISIBLE);
                ballView_twoline.setVisibility(View.VISIBLE);
                ballView_threeline.setVisibility(View.VISIBLE);
                ballView_firstLine.setSelect_max(100);
                ballView_twoline.setSelect_max(100);
                ballView_threeline.setSelect_max(100);
                break;
            case ZUSANDANSHI:
                lottery_wafa = ZUSANDANSHI;
                twoTitle.setVisibility(View.VISIBLE);
                threeTitle.setVisibility(View.GONE);
                twoTitle.setText(R.string.danhao);
                firstTitle.setText(R.string.chonghao);
                topView.setCenterText(string_zusandan);
                ballView_firstLine.setVisibility(View.VISIBLE);
                ballView_twoline.setVisibility(View.VISIBLE);
                ballView_threeline.setVisibility(View.GONE);
                ballView_firstLine.setSelect_max(1);
                ballView_twoline.setSelect_max(1);
                break;
            case ZUSANFUSHI:
                lottery_wafa = ZUSANFUSHI;
                twoTitle.setVisibility(View.GONE);
                threeTitle.setVisibility(View.GONE);
                firstTitle.setText(R.string.selectNum);
                topView.setCenterText(string_zusanfu);
                ballView_firstLine.setVisibility(View.VISIBLE);
                ballView_twoline.setVisibility(View.GONE);
                ballView_threeline.setVisibility(View.GONE);
                ballView_firstLine.setSelect_max(100);
                ballView_twoline.setSelect_max(100);
                ballView_threeline.setSelect_max(100);
                break;
            case ZULIU:
                lottery_wafa = ZULIU;
                twoTitle.setVisibility(View.GONE);
                threeTitle.setVisibility(View.GONE);
                firstTitle.setText(R.string.selectNum);
                topView.setCenterText(string_zuliu);
                ballView_firstLine.setVisibility(View.VISIBLE);
                ballView_twoline.setVisibility(View.GONE);
                ballView_threeline.setVisibility(View.GONE);
                ballView_firstLine.setSelect_max(100);
                ballView_twoline.setSelect_max(100);
                ballView_threeline.setSelect_max(100);
                break;
        }
        addMissValue();
    }

    @Override
    public void ballClick(WeiZhi info) {
        // TODO Auto-generated method stub
        WeiZhi w = info;
        // TODO Auto-generated method stub
        w.setType(0);
        int left = w.getLeft() - 5;
        int right = w.getRight() + 5;
        int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
                - (w.getBottom() - w.getTop()) - 10);
        int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
        Rect rect = new Rect(left, top, right, bottom);
        Log.e("FramLayout   画球区", ".............  gettype:" + w.getType());
        moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
    }

    @Override
    public void ballClick() {
        // TODO Auto-generated method stub
        if(LotteryIDUtils.FC3D_LOTTERY_ZUSAN_DAN_ID_INT == getPlayType()) {
            balls_threeLine.get(0).setSelected(true);
            for (int i = 0; i < balls_firstLine.size(); i++) {
                if (balls_firstLine.get(i).isSelected()) {
                    balls_twoLine.get(i).setSelected(false);
                    ballView_twoline.invalidate();
                }
            }
            for (int i = 0; i < balls_twoLine.size(); i++) {
                if (balls_twoLine.get(i).isSelected()) {
                    balls_firstLine.get(i).setSelected(false);
                    ballView_firstLine.invalidate();
                }
            }
        }
        int zhushu = getZhushu();
        int money = zhushu * 2;
        Log.e("fjl",zhushu+""+arr_infos.size()+ZhuShuFactory.getZhuShu(getPlayType()).getClass().getSimpleName());
        bottom_zhushu.setText(zhushu + "注 ");
        bottom_money.setText("共" + money + "米");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.shake_random_ll:
                randomBallNum();
                break;
        }
    }

    @Override
    public boolean touZhu() {
        // TODO Auto-generated method stub
        int zhuShu = getZhushu();
        if (zhuShu > 0) {
            int lotteryId = getTouZhuLotteryId();
            touZhu1(lotteryId, zhuShu);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onCenterClick() {
        super.onCenterClick();
        popFC3DSelectPlayMethod.showAsDropDown(topView);
    }

    public int getPlayType() {
        int playType = 0;
        switch (lottery_wafa) {
            case ZHIXUAN:
                playType = LotteryIDUtils.FC3D_LOTTERY_COMMON_FU_ID_INT;
                break;
            case ZUSANDANSHI:
                playType = LotteryIDUtils.FC3D_LOTTERY_ZUSAN_DAN_ID_INT;
                break;
            case ZUSANFUSHI:
                playType = LotteryIDUtils.FC3D_LOTTERY_ZUSAN_FU_ID_INT;
                break;
            case ZULIU:
                playType = LotteryIDUtils.FC3D_LOTTERY_ZULIU_FU_ID_INT;
                break;
        }
        return playType;
    }

    public abstract int getTouZhuLotteryId();

    @Override
    public void randomBallNum() {
        // TODO Auto-generated method stub
        clear();
        switch (lottery_wafa) {
            case ZHIXUAN:
            case ZUSANDANSHI:
                int first = RandomFactory.getRandom(
                        LotteryIDUtils.FC3D_LOTTERY_ID_INT).randomDanHang(1)[0];
                int two = RandomFactory.getRandom(
                        LotteryIDUtils.FC3D_LOTTERY_ID_INT).randomDanHang(1)[0];
                int thire = RandomFactory.getRandom(
                        LotteryIDUtils.FC3D_LOTTERY_ID_INT).randomDanHang(1)[0];
                balls_firstLine.get(first).setSelected(true);
                balls_twoLine.get(two).setSelected(true);
                balls_threeLine.get(thire).setSelected(true);
                break;
            case ZUSANFUSHI:
                int arr[] = RandomFactory.getRandom(
                        LotteryIDUtils.FC3D_LOTTERY_ID_INT).randomDanHang(2);
                for (int i = 0; i < arr.length; i++) {
                    balls_firstLine.get(arr[i]).setSelected(true);
                }
                break;
            case ZULIU:
                int arr_zuliu[] = RandomFactory.getRandom(
                        LotteryIDUtils.FC3D_LOTTERY_ID_INT).randomDanHang(3);
                for (int i = 0; i < arr_zuliu.length; i++) {
                    L.v(TAG, "arr_zuliu[i]>>" + arr_zuliu[i]);
                    balls_firstLine.get(arr_zuliu[i]).setSelected(true);
                }
                break;
        }
        ballView_firstLine.invalidate();
        ballView_twoline.invalidate();
        ballView_threeline.invalidate();

        ballClick();
    }

    @Override
    protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems, boolean isWaitting) {
        ll_history.removeAllViews();
        ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems)
                .getView());
    }

    @Override
    public void clear() {
        bottom_zhushu.setText(0 + "注 ");
        bottom_money.setText("共" + 0 + "米");

        initBallList(balls_firstLine, 10, 0, true);
        initBallList(balls_twoLine, 10, 0, true);
        initBallList(balls_threeLine, 10, 0, true);

        ballView_firstLine.setSelect_count(0);
        ballView_twoline.setSelect_count(0);
        ballView_threeline.setSelect_count(0);

        ballView_firstLine.invalidate();
        ballView_twoline.invalidate();
        ballView_threeline.invalidate();

    }

    @Override
    public String[] initRightItems() {
        // TODO Auto-generated method stub
        return this.getResources().getStringArray(R.array.rights_fc3d);
    }

    @Override
    public int[] initRightItemsImg() {
        // TODO Auto-generated method stub
//		return new int[] { R.drawable.ssq_dianxuan, R.drawable.ssq_dianxuan,
//				R.drawable.ssq_dianxuan, R.drawable.ssq_zoushitu,
//				R.drawable.icon_jieshao };
        return new int[]{R.drawable.icon_zhixuan, R.drawable.icon_zu3,
                R.drawable.icon_zu6,
                R.drawable.icon_problem};
    }


    @Override
    protected void popupClickItem(int arg2) {
        popupId = arg2;
        switch (arg2) {
            case 0:
                onWFCheckChange(R.id.cb_selectplaymethod_normal, false);
                break;
            case 1:
                onWFCheckChange(R.id.cb_selectplaymethod_group3, false);
                break;
            case 2:
                onWFCheckChange(R.id.cb_selectplaymethod_group6, false);
                break;
            case 3:
                onWFCheckChange(R.id.cb_selectplaymethod_group7, false);
                break;
        }
    }

    @Override
    public void onWFCheckChange(int id, boolean isChecked) {
        clear();
        switch (id) {
            case R.id.cb_selectplaymethod_normal:
                setLotteryBallView(ZHIXUAN);
                popupId = 0;
                break;
            case R.id.cb_selectplaymethod_group3:
                setLotteryBallView(ZUSANFUSHI);
                popupId = 1;
                break;
            case R.id.cb_selectplaymethod_group6:
                setLotteryBallView(ZULIU);
                popupId = 2;
                break;
            case R.id.cb_selectplaymethod_group7:
                setLotteryBallView(ZUSANDANSHI);
                popupId = 3;
                break;
        }
        popFC3DSelectPlayMethod.dismiss();
    }

    @Override
    protected void addMissValue() {
        super.addMissValue();
        if (missInfo != null) {
            switch (lottery_wafa) {
                case ZHIXUAN:
                    ballView_firstLine.setMissValues(missInfo.missValue(getLotteryId() + "MV_ZX").get(0));
                    ballView_twoline.setMissValues(missInfo.missValue(getLotteryId() + "MV_ZX").get(1));
                    ballView_threeline.setMissValues(missInfo.missValue(getLotteryId() + "MV_ZX").get(2));
                    break;
                case ZUSANDANSHI:
                    ballView_firstLine.setMissValues(missInfo.missValue(getLotteryId() + "MV_Z3").get(0));
                    ballView_twoline.setMissValues(missInfo.missValue(getLotteryId() + "MV_Z3").get(1));
                    break;
                case ZUSANFUSHI:
                    ballView_firstLine.setMissValues(missInfo.missValue(getLotteryId() + "MV_Z36").get(0));
                    break;
                case ZULIU:
                    ballView_firstLine.setMissValues(missInfo.missValue(getLotteryId() + "MV_Z36").get(0));
                    break;
            }
        }


    }

    @Override
    public Intent getTrendIntent() {
        Intent intent = new Intent(getContext(), PL3TrendActivity.class);
        intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
        return intent;
    }
}